package com.icode.dao.impl;

import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.Assignment;
import com.icode.core.model.house.MyHouse;
import com.icode.core.model.house.MyHouseStatus;
import com.icode.core.model.house.Rent;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.AssignmentDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-11-15
 * Time: 下午10:13
 * To change this template use File | Settings | File Templates.
 */
@Repository("assignmentDao")
public class AssignmentDaoImpl extends EntityDaoImpl implements AssignmentDao {
    @Override
    public List<Assignment> findAssignmentByStatus(MyHouseStatus myHouseStatus) {
        String hql = "from " + Assignment.class.getName() + " t where t.myHouseStatus=:status and t.active=true";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", myHouseStatus);
        return query.list();
    }

    @Override
    public Assignment getAssignmentsByMyHouse(MyHouse myHouse) {
        String hql = "from " + Assignment.class.getName() + " t where t.myHouse=:myHouse and t.active=true";
        Query query = currentSession().createQuery(hql);
        query.setParameter("myHouse",myHouse);
        List<Assignment> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<Assignment> findRentAllByPage(String currentPage, String status) {
        String hql;
        Query query;
        if (status.equals("ALL")) {
            hql = "from " + Assignment.class.getName();
            query = currentSession().createQuery(hql);

        } else {
            hql = "from " + Assignment.class.getName() + " a where a.myHouseStatus=:status";
            query = currentSession().createQuery(hql);
            switch (status) {
                case "TOASSIGNMNET":
                    query.setParameter("status", MyHouseStatus.TOASSIGNMNET);
                    break;
                case "ASSIGNMENTED":
                    query.setParameter("status", MyHouseStatus.ASSIGNMENTED);
                    break;
            }
        }
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageOfData);
        query.setMaxResults(PageDTO.pageOfData);
        List<Assignment> assignments = query.list();
        return assignments.isEmpty() ? null : assignments;
    }
}
